// CUser32.cpp : Defines the exported functions for the DLL application.
//

#include "stdafx.h"
#include "CUser32.h"
#include <Windows.h>
#include <map>
#include <Python.h>
#include <assert.h>
#include "PyCallback.h"

std::map<UINT, PyObject*> g_pyTimerProcMap;

void __stdcall _OnPyTimerProc(HWND hwnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime)
{
	std::map<UINT, PyObject*>::iterator iter;
	iter = g_pyTimerProcMap.find(idEvent);
	if (iter == g_pyTimerProcMap.end())
	{
		assert(0);
		return;
	}
	CallPyMethod(iter->second, "ii", idEvent, dwTime);

}
UINT SetPyTimer(UINT timerID, UINT uElapse, PyObject* pyCallBack)
{
	if (!PyCallable_Check(pyCallBack))
	{
		assert(0);
		return 0;
	}
	UINT nTimerId = SetTimer(0, timerID, uElapse, _OnPyTimerProc);
	if (nTimerId == 0)
	{
		assert(0);
		return 0;
	}
	std::map<UINT, PyObject*>::iterator iter;
	iter = g_pyTimerProcMap.find(nTimerId);
	if (iter != g_pyTimerProcMap.end())
	{
		Py_XDECREF(iter->second);
		iter->second = NULL;
	}
	g_pyTimerProcMap[nTimerId] = pyCallBack;
	Py_XINCREF( pyCallBack);
	return nTimerId;
}
BOOL CPeekMessage(MSG* lpMsg, long hwnd, UINT wMsgFilterMin, UINT wMsgFilterMax, UINT wRemoveMsg)
{
	return PeekMessageW(lpMsg, (HWND)hwnd, wMsgFilterMin, wMsgFilterMax, wRemoveMsg);
}
BOOL CGetMessage(LPMSG lpMsg, long hwnd, UINT wMsgFilterMin, UINT wMsgFilterMax)
{
	return GetMessageW(lpMsg, (HWND)hwnd, wMsgFilterMin, wMsgFilterMax);
}
BOOL CTranslateMessage(const MSG *lpMsg)
{
	return TranslateMessage(lpMsg);
}
void CDispatchMessage(const MSG *lpMsg)
{
	DispatchMessageW(lpMsg);
}


